package com.southwind.springboottest.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southwind.springboottest.entity.Book;
import com.southwind.springboottest.service.BookService;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/book")
public class BookHandler {

    @Autowired
    private BookService bookService;

    @Autowired
 private RedissonClient redissonClient;

    @GetMapping("/getbyid/{id}")
    public R getById(@PathVariable Integer id) {//参数来源于路径

        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("rw-lock");
        RLock rLock = readWriteLock.readLock();
        rLock.lock();
        Book book=null;
        try {
             book = bookService.getById(id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            rLock.unlock();
        }
        return new R(book);
    }

    @GetMapping("/getall/{page}/{size}")
    public R getall(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {

        //获取读写锁
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("rw-lock");
        //加读锁
        RLock rLock = readWriteLock.readLock();
        rLock.lock();
        Page<Book> bookPage = null;
        try {
            bookPage = bookService.getall(page, size);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            rLock.unlock();//释放锁
        }
        return new R(bookPage);
    }

    @PutMapping("/update")
    public R update(@RequestBody Book book) {
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("rw-lock");
        Integer integer=-1;

        //加写锁
        RLock rLock = readWriteLock.writeLock();
        rLock.lock();
        try {

            integer = bookService.updateById(book);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            rLock.unlock();
        }
        return new R(integer);
    }

    @PostMapping("/save")
    public R save(@RequestBody Book book) {  //注解将json转为java对象
        Integer integer=null;
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("rw-lock");
        RLock rLock = readWriteLock.writeLock();
        rLock.lock();
        try {
            integer = bookService.addBook(book);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            rLock.unlock();
        }
        return new R(integer);
    }

    @GetMapping("/findById/{id}")
    public R findById(@PathVariable("id") Integer id) {
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("rw-lock");
        RLock rLock = readWriteLock.readLock();
        rLock.lock();
        Book book = null;
        try {
            book = bookService.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            rLock.unlock();
        }
        return new R(book);

    }

    @DeleteMapping("/deletebyid/{id}")
    public R deleteById(@PathVariable("id") Integer id) {
        Integer integer=-1;
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("rw-lock");
        RLock rLock = readWriteLock.writeLock();
        rLock.lock();
        try {
            integer= bookService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            rLock.unlock();
        }
        return new R(integer);
    }

}
